package com.idp.web.test.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.xml.datatype.XMLGregorianCalendar;

import org.springframework.stereotype.Service;

import com.idp.common.persistence.Page;
import com.idp.web.test.dao.AmazonorderslistsDao;
import com.idp.web.test.dao.OrderItemDao;
import com.idp.web.test.dao.OrdertDao;
import com.idp.web.test.dao.PersonDao;
import com.idp.web.test.dao.ProductDao;
import com.idp.web.test.entity.Amazonorderslists;
import com.idp.web.test.entity.OrderItem;
import com.idp.web.test.entity.Ordert;
import com.idp.web.test.entity.Person;
import com.idp.web.test.entity.Product;
import com.idp.web.test.service.AmazonorderslistsService;

/**
 * 
 * Amazon订单导出Excelservice实现类
 * 
 * <pre>
 * 	历史记录：
 * 	2017-06-15 LiCongCong
 * 	新建文件
 * </pre>
 * 
 * @author
 * 
 *         <pre>
 * SD
 * 	LiCongCong
 * PG
 *	LiCongCong
 * UT
 *
 * MA
 *         </pre>
 * 
 * @version $Rev$
 *
 *          <p/>
 *          $Id$
 *
 */
@Service("amazonorderslistsService")
public class AmazonorderslistsServiceImpl implements AmazonorderslistsService {

	@Resource
	private AmazonorderslistsDao amazonorderslistsDao;

	@Resource
	private OrdertDao ordertDao;

	@Resource
	private ProductDao productDao;

	@Resource
	private PersonDao personDao;

	@Resource
	private OrderItemDao orderItemDao;

	@Override
	public Page<Amazonorderslists> findByPage(Amazonorderslists amazonorderslists, Page<Amazonorderslists> page) {

		page.setResult(amazonorderslistsDao.find(amazonorderslists, page));

		return page;
	}

	@Override
	public List<Amazonorderslists> findBySearch(Amazonorderslists amazonorderslists) {

		return amazonorderslistsDao.find(amazonorderslists);
	}

	@Override
	public Amazonorderslists getById(Integer id) {

		return amazonorderslistsDao.getById(id);
	}

	@Override
	public void add(Amazonorderslists amazonorderslists) {

		amazonorderslists.setId(amazonorderslists.getId());
		amazonorderslistsDao.add(amazonorderslists);
	}

	@Override
	public void update(Amazonorderslists amazonorderslists) {

		amazonorderslistsDao.update(amazonorderslists);
	}

	@Override
	public void delete(Integer id) {

		amazonorderslistsDao.delete(id);
	}

	public static Date xmlDate2Date(XMLGregorianCalendar cal) {
		return cal.toGregorianCalendar().getTime();
	}

	@Override
	public Integer synchronization(List<Amazonorderslists> olist, Integer platformId,Integer accountidd) {

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Integer i = 0;
		Date date = ordertDao.getCreateTimeByPlatformDesc(platformId);
		Date orderDate = null;
		Person person = new Person();
		Product product = new Product();
		Ordert ordert = new Ordert();
		OrderItem orderItem = new OrderItem();
		for (Amazonorderslists a : olist) {
			try {

				String xmldate = a.getPurchasedate();
				String newstr = xmldate.replace("T", " ");
				String datestr = newstr.replace("Z", "");

				System.err.println(datestr);

				orderDate = simpleDateFormat.parse(datestr);

				if (date == null || orderDate.after(date)) {

					person.setAddress(a.getShipcountry() + "," + a.getShipstate() + "," + a.getShipcity() + ","
							+ a.getShipaddress1() + "," + a.getShipaddress2() + "," + a.getShipaddress3());
					person.setName(a.getRecipientname());
					person.setPhone(a.getShipphonenumber() == null ? "未知" : a.getShipphonenumber());
					person.setUid(a.getRecipientname());

					person.setZipcode(a.getShippostalcode());

					product = productDao.getByCode(a.getSku());
					if (product == null) {
						product = new Product();
						product.setCode("SKU" + xmldate);
						product.setName("Name" + a.getOrderid());
						product.seteBayCode(a.getSku());

						product.setProductStatusId(1);

						productDao.add(product);
					}
					orderItem.setProductId(product.getId());
					orderItem.setSku("SKU" + xmldate);
					orderItem.setTotalPrice(Double.valueOf(a.getItemprice()));
					orderItem.setPrice(Double.valueOf(a.getItemprice()));
					orderItem.setCount(Integer.valueOf(a.getQuantitypurchased()));

					personDao.add(person);

					ordert.setCode(a.getOrderid());
					Integer orderstatus = 1;

					if (a.getOrderstatus().equals("Shipped")) {
						orderstatus = 2;
					} else if (a.getOrderstatus().equals("Unshipped")) {
						orderstatus = 1;
					} else if (a.getOrderstatus().equals("Pending")) {
						orderstatus = 3;
					} else if (a.getOrderstatus().equals("Canceled")) {
						orderstatus = 5;
					} else {
						orderstatus = 4;
					}
					ordert.setOrderStatusId(orderstatus);
					ordert.setCreatetime(datestr);
					ordert.setPlatformId(platformId);
					ordert.setWarehouseId(1);
					ordert.setPersonId(person.getId());
					ordert.setPrice(Double.valueOf(a.getItemprice()));
					ordert.setCount(Integer.parseInt(a.getQuantitypurchased()));
					ordert.setAccountId(accountidd);
					ordertDao.add(ordert);

					orderItem.setOrdertId(ordert.getId());
					System.out.println(orderItem);
					orderItemDao.add(orderItem);
					i++;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return i;
	}

}